package com.aia.prm.web.ria.app.ui.pext;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.aia.prm.domain.VOPext;
import com.aia.prm.domain.VOPextExample;
import com.aia.prm.domain.VOPlan;
import com.aia.prm.persistence.dao.VOPextDAO;
import com.aia.prm.persistence.dao.VOPlanDAO;
import com.aia.prm.web.ria.app.PrmApplication;
import com.cfh.util.Tools;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

@SuppressWarnings("serial")
public class PextView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(PextView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	PextList PextList = null;
	Form form = null;
	PextContainer PextContainer = null;
	PextForm PextForm = null;
	private VOPextDAO PextDAO = null;
	List<VOPext> list = null;

	VerticalLayout topLayout = null;
	Button newButton = null;
	Button batchButton = null;
	Button okButton = null;
	Label help = null;

	private ComboBox plansBox = null;

	public ComboBox getPlanBox() {
		return plansBox;
	}

	public PextView(PrmApplication aapp) {
		app = aapp;
		setPextDAO((VOPextDAO) BeanFactory.get().getBean("vOPextDAO"));
		init();
		buildMainLayout();
		load();
	}

	private void init() {
		plansBox = new ComboBox();
		VOPlanDAO vOPlanDAO = (VOPlanDAO) BeanFactory.get().getBean("vOPlanDAO");
		List list = vOPlanDAO.selectByExample(null);
		String firstItem = null;
		for (Object o : list) {
			VOPlan p = (VOPlan) o;
			if (firstItem == null) {
				firstItem = p.getPL_ID();
			}
			plansBox.addItem(p.getPL_ID());
			plansBox.setItemCaption(p.getPL_ID(), "[" + p.getPL_ID() + "]" + p.getPL_NAME());
		}
		if (firstItem != null) {
			plansBox.select(firstItem);
		}
		String helpContext = "<p><b>添加字段帮助：</b></p>"
				+ "<p>字段：可以输入以下选项 -- '姓名','性别','年龄','省市区','电子邮件','手机号码','手机验证码','营销员编码','自由输入字段','出生年','出生月','出生日','图片验证码'</p>"
				+ "<p>字段别名：显示的字段别名，例如'自由输入字段'可以起别名'保险需求'" + "<p>是否必填：如果是Y则显示为红星标记，输入较验时做为必填项" + "<p>排序号：输入数字格式，从小到大排序"
				+ "<p>* 建议：请点击一键添加所有字段然后删除掉不要的字段";
		help = new Label(helpContext, Label.CONTENT_XHTML);
	}

	void load() {

		String id = (String) plansBox.getValue();
		if (Tools.hasText(id)) {
			VOPextExample example = new VOPextExample();
			example.createCriteria().andPL_IDEqualTo(id);
			list = getPextDAO().selectByExample(example);

		} else {
			list = new ArrayList();
		}
		if (PextContainer == null) {
			try {
				PextContainer = new PextContainer();
			} catch (Throwable e) {
				logger.error(e);
				throw new RuntimeException(e);
			}
		}

		PextContainer.removeAllItems();
		PextContainer.addAll(list);

		PextList.setContainerDataSource(PextContainer);

		PextList.setVisibleColumns(PextContainer.NATURAL_COL_ORDER);
		PextList.setColumnHeaders(PextContainer.COL_HEADERS_SHOW);

		PextForm.setItemDataSource(null);
	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(60, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		topLayout = new VerticalLayout();
		topLayout.setSpacing(true);
		newButton = new Button("添加记录", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				PextForm.addObject();
			}
		});
		newButton.setIcon(new ThemeResource("icons/add.png"));
		batchButton = new Button("一键添加所有字段", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				String[] fields = new String[] { "姓名", "性别", "年龄", "省市区", "电子邮件", "手机号码", "手机验证码", "营销员编码", "自由输入字段", "出生年", "出生月", "出生日",
						"图片验证码" };
				String id = (String) plansBox.getValue();
				for (int i = 0; i < fields.length; i++) {
					try {
						VOPext record = new VOPext();
						record.setPL_ID(id);
						record.setEX_TYPE("字段");
						record.setEX_VALUE(fields[i]);
						record.setEX_ORDER((long) (i + 1));
						record.setEX_FREEFLAG1("Y");
						PextDAO.insert(record);
					} catch (Exception e) {
					}
				}
				load();
			}
		});
		batchButton.setIcon(new ThemeResource("icons/add.png"));
		// topLayout.addComponent(newButton);
		// topLayout.addComponent(plansBox);
		HorizontalLayout topLayoutSub = new HorizontalLayout();
		topLayoutSub.setSizeUndefined();
		topLayoutSub.setMargin(true);
		topLayoutSub.setSpacing(true);
		topLayoutSub.addComponent(new Label("选择产品:"));
		topLayoutSub.addComponent(plansBox);

		okButton = new Button("确定", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				logger.debug("选择了：" + plansBox.getValue());
				load();
			}
		});
		okButton.setIcon(new ThemeResource("icons/search.png"));
		topLayoutSub.addComponent(okButton);
		topLayoutSub.addComponent(newButton);
		topLayoutSub.addComponent(batchButton);
		Label l = new Label("*显示帮助");
		l.setDescription("该页面可以添加展示图片，请输入3个字段：图片id、图片描述、排序号。每个产品支持1至多个图片。");
		topLayoutSub.addComponent(l);

		topLayout.addComponent(topLayoutSub);
		vert.setFirstComponent(topLayout);

		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		PextList = new PextList(app, this);
		horiz.setFirstComponent(PextList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		PextForm = new PextForm(this);

		rightLayout.addComponent(PextForm);
		rightLayout.addComponent(help);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		Item item = PextList.getItem(PextList.getValue());
		PextForm.setItemDataSource(item);
	}

	public VOPextDAO getPextDAO() {
		return PextDAO;
	}

	public void setPextDAO(VOPextDAO PextDAO) {
		this.PextDAO = PextDAO;
	}

}
